// 从本地存储获取初始数据
const storedData = localStorage.getItem('receive-data')
const initialData = storedData ? JSON.parse(storedData) : {}

const useReceiveDataStore = defineStore(
  'receiveData',
  {
    state: () => ({
      // 仅包含receiveData一个状态
      receiveData: initialData
    }),
    actions: {
      // 设置接收的数据
      setReceiveData(data) {
        this.receiveData = data
        // 同时保存到本地存储
        localStorage.setItem('receive-data', JSON.stringify(data))
      },
      // 移除接收的数据
      removeReceiveData() {
        this.receiveData = {}
        // 同时从本地存储中移除
        localStorage.removeItem('receive-data')
      }
    }
  })

export default useReceiveDataStore
